package server

import (
	core "carmanager/db"
	model "carmanager/models"
	"carmanager/models/dto"
	"carmanager/models/vo"
	"fmt"
	"strings"
	"time"
)

type UserService struct{}

func (r *UserService) GetList(user model.User, parms dto.PageParms) vo.PageInfo {
	db := core.GetDB()
	start := (parms.PageIndex - 1) * parms.PageSize
	data := new([]model.User)
	Total := new(int64)
	query := db
	if !strings.EqualFold(user.UserName, "") {
		println(user.UserName)
		query = query.Where("userName like ?", "%"+user.UserName+"%")
	}
	query.Model(model.User{}).Count(Total)
	query.Limit(parms.PageSize).Offset(start).Find(data)

	return vo.PageInfo{
		TotalNumber: *Total,
		Data:        data,
	}
}
func (r *UserService) GetUserInfo(userid int) *model.User {
	user := new(model.User)
	core.Db.Where("id=?", userid).First(user)
	return user
}
func (r *UserService) Update(model model.User) bool {
	db := core.GetDB()
	fmt.Println(db.Update("", &model).Error)
	return db.Updates(&model).Error != nil
}
func (r *UserService) Add(model model.User) bool {
	db := core.GetDB()
	model.Datetime = time.Now()
	if model.Roleid == 0 {
		//默认为普通用户
		model.Roleid = 2
	}
	return db.Create(&model).Error != nil
}
func (r *UserService) Del(id int) bool {
	db := core.GetDB()
	return db.Model(&model.User{}).Delete("id=?", id).Error != nil
}
